package it.unibz.easytravel.itemlist;

import it.unibz.easytravel.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;

public class TabSelectItems extends Activity {
	private LinearLayout mSelectCheckbox, mUnSelectCheckbox;
	private ItemList untaken = new ItemList();
	private ItemList taken = new ItemList();
	private void initList(){
		untaken.add(new Item("Rope", true, false));
		untaken.add(new Item("Pot", true, false));
		untaken.add(new Item("Water", false, false));
		untaken.add(new Item("Food", false, false));
		untaken.add(new Item("Tent", false, false));

	}
	private void displayList() {
		// Get list from previous view
		//untaken = DisplayItemList.getList();
		mSelectCheckbox = (LinearLayout) findViewById(R.id.SelectList_checkbox);
		int length = untaken.size();
		for (int i = 0; i < length; i++) {
			CheckBox newCheckBox = new CheckBox(this);
			newCheckBox.setText(((Item) untaken.elementAt(i)).name);
			newCheckBox.setId(i);
			mSelectCheckbox.addView(newCheckBox);
		}
		
		mUnSelectCheckbox = (LinearLayout) findViewById(R.id.UnSelectList_checkbox);
		length = taken.size();
		for (int i = 0; i < length; i++) {
			CheckBox newCheckBox = new CheckBox(this);
			newCheckBox.setText(((Item) taken.elementAt(i)).name);
			newCheckBox.setId(i);
			mUnSelectCheckbox.addView(newCheckBox);
		}
		
	}

	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		initList();
		setContentView(R.layout.itemlist_selectitems);
		displayList();
		
		Button selectButton = (Button) findViewById(R.id.Button_SelectItem);
		selectButton.setOnClickListener(selectButtonListener);

		Button unselectButton = (Button) findViewById(R.id.Button_UnselectItem);
		unselectButton.setOnClickListener(unselectButtonListener);

	}

	private OnClickListener selectButtonListener = new OnClickListener() {
		public void onClick(View v) {
			CheckBox item;
			// String name = "";
			int length = untaken.size();
			for (int i = 0; i < length; i++) {
				item = (CheckBox) mSelectCheckbox.getChildAt(i);
				if (item.isChecked()) {
					taken.add(item.getText().toString());
					untaken.remove(item.getText().toString());
					item.setChecked(false);
					// mListCheckbox.removeViewAt(i);
				}
			}
			mSelectCheckbox.removeAllViews();
			mUnSelectCheckbox.removeAllViews();
			displayList();

		}
	};

	private OnClickListener unselectButtonListener = new OnClickListener() {
		public void onClick(View v) {
			CheckBox item;
			// String name = "";
			int length = taken.size();
			for (int i = 0; i < length; i++) {
				item = (CheckBox) mUnSelectCheckbox.getChildAt(i);
				if (item.isChecked()) {
					untaken.add(item.getText().toString());
					taken.remove(item.getText().toString());
					item.setChecked(false);
					// mListCheckbox.removeViewAt(i);
				}
			}
			mSelectCheckbox.removeAllViews();
			mUnSelectCheckbox.removeAllViews();
			displayList();

		}
	};

}
